<?php

namespace App\Api\Services;

use App\Api\Models\LogInStorageModel;
use App\Api\Models\LogOutStorageModel;
use App\Api\Models\LogPackageModel;
use Illuminate\Support\Facades\DB;

class LogService
{
    //操作记录详细
    static public function detail($data)
    {
        switch($data['type']) {
            case 0:
                $result = SELF::logInstorageDetail($data);
                break;
            case 1:
                $result = SELF::logOutstorageDetail($data);
                break;
            case 2:
                $result = SELF::logPackageContentDetail($data);
                break;
        }

        return $result;
    }

    static private function logInstorageDetail($data)
    {
        return LogInstorageModel::select(
            'a.id',
            'a.type',
            'f.storage_name as last_storage_name',
            'f.storage_address as last_storage_address',
            'e.storage_name',
            'e.storage_address',
//            'a.materiel_class_name',
//            'a.materiel_class_no',
            'g.materiel_name',
            'g.type_name',
            'g.materiel_no',
            'a.operate_num',
            'a.last_storage_num',
            'a.next_storage_num',
            'a.materiel_mean_cost',
            'a.all_id_type',
            'a.remark',
            'a.is_cancel',
            'a.in_storage_time',
            'a.purchase_no',
            'b.class_name as materiel_class_name',
            'b.class_no as materiel_class_no',
            'c.class_name',
            'c.class_no',
            'd.username',
            'd.nickname'
        )
            ->from('log_instorage as a')
            ->leftJoin('materiel_class as b', 'b.id', '=', 'a.materiel_class_id')
            ->leftJoin('materiel_class as c', 'b.parent_id', '=', 'c.id')
            ->leftJoin('admin as d', 'd.id', '=', 'a.user_id')
            ->leftJoin('storage as e', 'e.id', '=', 'a.storage_id')
            ->leftJoin('storage as f', 'f.id', '=', 'a.last_storage_id')
            ->leftJoin('materiel_type as g', 'g.id', '=', 'a.materiel_type_id')
            ->where([
                'a.id' => $data['id'],
            ])
            ->first();
    }

    //出库操作记录详细
    static private function logOutstorageDetail($data)
    {
        return LogOutstorageModel::select(
            'a.id',
            'a.type',
            'f.storage_name as next_storage_name',
            'f.storage_address as next_storage_address',
            'e.storage_name',
            'e.storage_address',
//                'a.materiel_class_name',
//                'a.materiel_class_no',
            'g.materiel_name',
            'g.type_name',
            'g.materiel_no',
            'a.operate_num',
            'a.last_storage_num',
            'a.next_storage_num',
            'a.materiel_mean_cost',
            'a.remark',
            'a.all_id_type',
            'a.is_cancel',
            'a.out_storage_time',
            'b.class_name as materiel_class_name',
            'b.class_no as materiel_class_no',
            'c.class_name',
            'c.class_no',
            'd.username',
            'd.nickname'
        )
            ->from('log_outstorage as a')
            ->leftJoin('materiel_class as b', 'b.id', '=', 'a.materiel_class_id')
            ->leftJoin('materiel_class as c', 'b.parent_id', '=', 'c.id')
            ->leftJoin('admin as d', 'd.id', '=', 'a.user_id')
            ->leftJoin('storage as e', 'e.id', '=', 'a.storage_id')
            ->leftJoin('storage as f', 'f.id', '=', 'a.next_storage_id')
            ->leftJoin('materiel_type as g', 'g.id', '=', 'a.materiel_type_id')
            ->where([
                'a.id' => $data['id']
            ])
            ->first();
    }

    //套餐出库记录详细
    static public function logPackageContentDetail($data)
    {
        $datas = LogPackageModel::select(
            'a.id',
            'c.package_name',
            'num',
            'remark',
            'a.created_at',
            'username',
            'nickname',
            'is_cancel',
            'a.remark'
        )
            ->from('log_package as a')
            ->leftJoin('admin as b', 'b.id', '=', 'a.user_id')
            ->with('hasManyContent', function($query) {
                $query->select(
                    'log_package_content.id',
                    'log_package_id',
                    'a.storage_name',
                    'a.storage_address',
                    'b.class_name as second_class_name',
                    'c.class_name as first_class_name',
                    'd.materiel_name',
                    'd.type_name',
                    'operate_num',
                    'e.in_storage_num'
                )
                    ->leftJoin('storage as a', 'a.id', '=', 'log_package_content.storage_id')
                    ->leftJoin('materiel_class as b', 'b.id', '=', 'log_package_content.materiel_class_id')
                    ->leftJoin('materiel_class as c', 'b.parent_id', '=', 'c.id')
                    ->leftJoin('materiel_type as d', 'd.id', '=', 'log_package_content.materiel_type_id')
                    ->leftJoin('storage_manage as e', 'e.id', '=', 'log_package_content.out_storage_manage_id');
            })
            ->leftJoin('package as c', 'c.id', '=', 'a.package_id')
            ->where('a.id', $data['id'])
            ->get()->toArray();

        $arr['id'] = $datas[0]['id'];
        $arr['package_name'] = $datas[0]['package_name'];
        $arr['num'] = $datas[0]['num'];
        $arr['remark'] = $datas[0]['remark'];
        $arr['created_at'] = $datas[0]['created_at'];
        $arr['username'] = $datas[0]['username'];
        $arr['nickname'] = $datas[0]['nickname'];
        $arr['is_cancel'] = $datas[0]['is_cancel'];
        $arr['content'] = $datas[0]['has_many_content'];

        return $arr;
    }


    static public function logDel($data, $logIdArr, $condition)
    {
        switch($data['type']) {
            //入库
            case 0:
                $result = SELF::instorageDel($logIdArr, $condition);
                break;
            //出库
            case 1:
                $result = SELF::outstorageDel($logIdArr, $condition);
                break;
            //套餐出库
            case 2:
                $result = SELF::packageOutstorageDel($logIdArr, $condition);
                break;
        }

        return $result;
    }

    static private function instorageDel($logIdArr, $condition)
    {
        foreach($logIdArr as $value) {
            $logInfo = LogInstorageModel::where([
                'id' => $value,
                'delete_flag' => 0
            ])
                ->where($condition)
                ->first();

            if(empty($logInfo)) {
                return ['code' => 1, 'msg' => '目标日志不存在,请重试', 'data' => ''];
            }

            if(!$logInfo['is_cancel']) {
                return ['code' => 1, 'msg' => '目标日志未撤销，不可删除', 'data' => ''];
            }
        }

        LogInstorageModel::where('id', $logIdArr)->update(['delete_flag' => 1]);

        return ['code' => 0, 'msg' => 'success', 'data' => ''];
    }

    static private function outstorageDel($logIdArr, $condition)
    {
        foreach($logIdArr as $value) {
            $logInfo = LogOutstorageModel::where([
                'id' => $value,
                'delete_flag' => 0
            ])
                ->where($condition)
                ->first();

            if(empty($logInfo)) {
                return ['code' => 1, 'msg' => '目标日志不存在,请重试', 'data' => ''];
            }

            if(!$logInfo['is_cancel']) {
                return ['code' => 1, 'msg' => '目标日志未撤销，不可删除', 'data' => ''];
            }
        }

        LogOutstorageModel::where('id', $logIdArr)->update(['delete_flag' => 1]);

        return ['code' => 0, 'msg' => 'success', 'data' => ''];
    }

    static private function packageOutstorageDel($logIdArr, $condition)
    {
        foreach($logIdArr as $value) {
            $logInfo = LogPackageModel::where([
                'id' => $value,
                'delete_flag' => 0
            ])
                ->where($condition)
                ->first();

            if(empty($logInfo)) {
                return ['code' => 1, 'msg' => '目标日志不存在,请重试', 'data' => ''];
            }

            if(!$logInfo['is_cancel']) {
                return ['code' => 1, 'msg' => '目标日志未撤销，不可删除', 'data' => ''];
            }
        }

        LogPackageModel::where('id', $logIdArr)->update(['delete_flag' => 1]);

        return ['code' => 0, 'msg' => 'success', 'data' => ''];
    }
}
